package com.grm.distributed.lock.controller;

import com.grm.distributed.lock.common.Result;
import com.grm.distributed.lock.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    private ProductService15 productService15;
    @Autowired
    private ProductService14 productService14;
    @Autowired
    private ProductService13 productService13;
    @Autowired
    private ProductService12 productService12;

    @Autowired
    private ProductService11 productService11;

    @GetMapping("/sell")
    public Result sellProduct(){
        return productService11.sell();
    }

    @GetMapping("/fair/lock/{id}")
    public Result testFairLock(@PathVariable Long id){
        productService12.testFairLock(id);
        return Result.success("hello fair lock : "+id);
    }

    @GetMapping("/read/lock")
    public Result testReadLock(){
        productService13.testReadLock();
        return Result.success("hello read lock");
    }

    @GetMapping("/write/lock")
    public Result testWriteLock(){
        productService13.testWriteLock();
        return Result.success("hello write lock");
    }

    @GetMapping("/semaphore/{id}")
    public Result testSemaphore(@PathVariable Integer id){
        productService14.testSemaphore(id);
        return Result.success("hello semaphore,"+id);
    }

    @GetMapping("/test/latch")
    public String testLatch(){
        productService15.testLatch();
        return "班长锁门。。。";
    }

    // 请求6次，latch请求才会响应
    @GetMapping("/test/countdown")
    public String testCountDown(){
        productService15.testCountDown();
        return "出来了一位同学";
    }
}
